package com.zhs.base.utils;

import org.apache.commons.io.FileUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

/**
 * @author Java架构师之旅
 * @date 2022/8/3 17:06
 */
public class MyFileUtils {
    public static File createTmpFile(final BufferedInputStream inputStream, final String name, final String ext) throws IOException {
        return createTmpFile(inputStream, name, ext, Files.createTempDirectory("demo").toFile());
    }

    public static File createTmpFile(InputStream inputStream, String name, String ext, File tmpDirFile) throws IOException {
        File resultFile = File.createTempFile(name, '.' + ext, tmpDirFile);
        resultFile.deleteOnExit();
        FileUtils.copyToFile(inputStream, resultFile);
        return resultFile;
    }
}
